<template>
  <div class="workstation-detail">
    <el-card class="detail-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span class="title">工位详情</span>
          <el-button type="primary" @click="goBack">返回列表</el-button>
        </div>
      </template>

      <div class="detail-content" v-if="workstationData">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="工位编码">
            {{ workstationData.workstationCode }}
          </el-descriptions-item>
          <el-descriptions-item label="工位名称">
            {{ workstationData.workstationName }}
          </el-descriptions-item>
          <el-descriptions-item label="所属工厂">
            {{ workstationData.factoryName }}
          </el-descriptions-item>
          <el-descriptions-item label="所属车间">
            {{ workstationData.workshopName }}
          </el-descriptions-item>
          <el-descriptions-item label="所属产线">
            {{ workstationData.productionLineName }}
          </el-descriptions-item>
          <el-descriptions-item label="工位类型">
            <el-tag :type="getTypeColor(workstationData.workstationType)">
              {{ formatWorkstationType(workstationData.workstationType) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="工位状态">
            <el-tag :type="getStatusType(workstationData.status)">
              {{ formatWorkstationStatus(workstationData.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="产能/小时">
            {{ workstationData.capacity || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="位置序号">
            {{ workstationData.position || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="位置">
            {{ workstationData.location }}
          </el-descriptions-item>
          <el-descriptions-item label="支持工艺类型" :span="2">
            {{ workstationData.supportedProcessTypes || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="描述" :span="2">
            {{ workstationData.description || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ workstationData.createTime }}
          </el-descriptions-item>
          <el-descriptions-item label="更新时间">
            {{ workstationData.updateTime }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="WorkstationDetail">
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { getWorkstationById, type Workstation } from "@/api/modules/workstation";

const route = useRoute();
const router = useRouter();
const loading = ref(false);
const workstationData = ref<Workstation.WorkstationListItem | null>(null);

// 格式化工位类型
const formatWorkstationType = (type: string) => {
  const types: Record<string, string> = {
    MANUAL: "手动",
    AUTOMATIC: "自动",
    SEMI_AUTOMATIC: "半自动"
  };
  return types[type] || type;
};

// 格式化工位状态
const formatWorkstationStatus = (status: string) => {
  const statuses: Record<string, string> = {
    IDLE: "空闲",
    RUNNING: "运行中",
    FAULT: "故障",
    MAINTENANCE: "维护中"
  };
  return statuses[status] || status;
};

// 获取类型颜色
const getTypeColor = (type: string) => {
  const typeMap: Record<string, string> = {
    MANUAL: "primary",
    AUTOMATIC: "success",
    SEMI_AUTOMATIC: "warning"
  };
  return typeMap[type] || "info";
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    IDLE: "info",
    RUNNING: "success",
    FAULT: "danger",
    MAINTENANCE: "warning"
  };
  return statusMap[status] || "info";
};

// 获取工位详情
const getWorkstationDetail = async () => {
  const id = route.params.id as string;
  if (!id) {
    ElMessage.error("工位ID不能为空");
    goBack();
    return;
  }

  loading.value = true;
  try {
    const res = await getWorkstationById(id);
    workstationData.value = res.data;
  } catch (error) {
    ElMessage.error("获取工位详情失败");
    console.error(error);
  } finally {
    loading.value = false;
  }
};

// 返回列表
const goBack = () => {
  router.push("/workstation");
};

onMounted(() => {
  getWorkstationDetail();
});
</script>

<style scoped>
.workstation-detail {
  padding: 20px;
}

.detail-card {
  max-width: 1200px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: 600;
}

.detail-content {
  padding: 20px 0;
}

:deep(.el-descriptions__label) {
  font-weight: 600;
  width: 120px;
}
</style>